package com.libraryApp.service.impl;

import com.libraryApp.appUtil.BeanUtil;
import com.libraryApp.appUtil.page.PageQueryUtil;
import com.libraryApp.appVo.AppUserBorrowVo;
import com.libraryApp.mapper.AppBorrowMapper;
import com.libraryApp.service.AppBorrowService;
import com.libraryManage.commons.utils.MVCUtil;
import com.libraryManage.commons.utils.PageUtil;
import com.libraryManage.commons.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * @CreateDate 2021-03-08
 * @Author yufang
 */
@Service
public class AppBorrowServiceImpl implements AppBorrowService {

    @Autowired
    AppBorrowMapper appBorrowMapper;


//    public String borrowList(PageQueryUtil pageUtil) {
//
//        int total = newBeeMallOrderMapper.getTotalNewBeeMallOrders(pageUtil);
//        List<NewBeeMallOrder> newBeeMallOrders = newBeeMallOrderMapper.findNewBeeMallOrderList(pageUtil);
//        List<NewBeeMallOrderListVO> orderListVOS = new ArrayList<>();
//        if (total > 0) {
//            //数据转换 将实体类转成vo
//            orderListVOS = BeanUtil.copyList(newBeeMallOrders, NewBeeMallOrderListVO.class);
//            //设置订单状态中文显示值
//            for (NewBeeMallOrderListVO newBeeMallOrderListVO : orderListVOS) {
//                newBeeMallOrderListVO.setOrderStatusString(NewBeeMallOrderStatusEnum.getNewBeeMallOrderStatusEnumByStatus(newBeeMallOrderListVO.getOrderStatus()).getName());
//            }
//            List<Long> orderIds = newBeeMallOrders.stream().map(NewBeeMallOrder::getOrderId).collect(Collectors.toList());
//            if (!CollectionUtils.isEmpty(orderIds)) {
//                List<NewBeeMallOrderItem> orderItems = newBeeMallOrderItemMapper.selectByOrderIds(orderIds);
//                Map<Long, List<NewBeeMallOrderItem>> itemByOrderIdMap = orderItems.stream().collect(groupingBy(NewBeeMallOrderItem::getOrderId));
//                for (NewBeeMallOrderListVO newBeeMallOrderListVO : orderListVOS) {
//                    //封装每个订单列表对象的订单项数据
//                    if (itemByOrderIdMap.containsKey(newBeeMallOrderListVO.getOrderId())) {
//                        List<NewBeeMallOrderItem> orderItemListTemp = itemByOrderIdMap.get(newBeeMallOrderListVO.getOrderId());
//                        //将NewBeeMallOrderItem对象列表转换成NewBeeMallOrderItemVO对象列表
//                        List<NewBeeMallOrderItemVO> newBeeMallOrderItemVOS = BeanUtil.copyList(orderItemListTemp, NewBeeMallOrderItemVO.class);
//                        newBeeMallOrderListVO.setNewBeeMallOrderItemVOS(newBeeMallOrderItemVOS);
//                    }
//                }
//            }
//        }
//        PageResult pageResult = new PageResult(orderListVOS, total, pageUtil.getLimit(), pageUtil.getPage());
//        return pageResult;
//
//    }

    /**
     * 查询借阅记录
     * @param userId
     * @param pageNumber
     * @param status
     * @return
     */
    @Override
    public ResponseEntity<List<AppUserBorrowVo>> borrowList(Long userId, Integer pageNumber, Integer status) {

        AppUserBorrowVo vo = new AppUserBorrowVo();
        vo.setPageNum(pageNumber);
        vo.setPageSize(5);
        PageUtil.startPage(vo);
        List<AppUserBorrowVo> list = appBorrowMapper.borrowList(userId,status);

        return PageUtil.tranferPage(list);
    }
}
